var net = require('net');

const portIsOccupied = port => new Promise((resolve, reject) => {
  const server = net.createServer().listen(port);
  server.on('listening', function () { // 执行这块代码说明端口未被占用
    server.close(); // 关闭服务
    resolve()
  });

  server.on('error', function (err) {
    reject()
  })
});

const getPort = async function () {
  for (var port = 8010; port < 8099; port++) {
    try {
      const a = await portIsOccupied(port);
      return port
    } catch (e) {

    }
  }
};

module.exports = {
  getPort: getPort
};

